<title>后台管理员</title>

<div class="layui-card layadmin-header">
    <div class="layui-breadcrumb" lay-filter="breadcrumb">
        <a lay-href="">主页</a>
        <a><cite>产品中心</cite></a>
    </div>
</div>

<body>
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-form layui-card-header layuiadmin-card-header-auto" lay-filter="layadmin-userfront-formlist">
                <div class="demoTable">
                    分类ID：
                    <div class="layui-inline">
                        <select name="categoryId" id="categoryId" lay-verify="categoryId" lay-search="">
                        </select>
                    </div>
                    是否热门：
                    <div class="layui-input-inline">
                        <select name="isHot" lay-search="">
                            <option value="">请选择</option>
                            <option value="1">是</option>
                            <option value="0">否</option>
                        </select>
                    </div>
                    产品名：
                    <div class="layui-inline">
                        <input class="layui-input" name="productName" id="productName" autocomplete="off">
                    </div>
                    目录号：
                    <div class="layui-inline">
                        <input class="layui-input" name="indexNo" id="indexNo" autocomplete="off">
                    </div>
                    <button class="layui-btn" data-type="reload" lay-submit lay-filter="LAY-user-front-search">搜索</button>
                    <script type="text/html" id="toolbarDemo">
                        <div class="layui-btn-container">
                            <button type="button" class="layui-btn" id="addBtn" lay-event="addBtn">添加信息</button>
                            <button type="button" class="layui-btn" id="sonBtn" lay-event="sonBtn">添加子目录</button>
                            <button type="button" class="layui-btn" lay-event="getCheckLength" id="deleBtn">批量删除</button>
                        </div>
                    </script>

                </div>
            </div>
            <div class="layui-fluid">
                <div class="layui-card">
                    <div class="layui-card-body">
                        <table class="layui-hide" id="table-instance" lay-filter="table-instance"></table>
                        <script type="text/html" id="table-useradmin-webuser">
                            <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">查看</a>
                            <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
                            <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
                        </script>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
<script>
    layui.use(['layedit', 'admin', 'upload', 'laydate', 'useradmin', 'table', 'form'], function() {
        var $ = layui.jquery,
            admin = layui.admin,
            setter = layui.setter,
            view = layui.view,
            table = layui.table,
            form = layui.form,
            leyer = layui.leyer,
            upload = layui.upload,
            laydate = layui.laydate,
            layedit = layui.layedit;

        form.render(null, 'layadmin-userfront-formlist');
        //监听搜索
        form.on('submit(LAY-user-front-search)', function(data) {
            var field = data.field;
            //执行重载
            table.reload('table-instance', {
                where: field
            });
        });
        //监听工具栏操作（新增，批量删除等）
        table.on('toolbar(table-instance)', function(obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'addBtn':
                    admin.popup({
                        title: '添加产品',
                        area: ['900px', '800px'],
                        offset: 'auto',
                        id: 'LAY-popup-useradmin-add',
                        url: setter.request.baseUrl + "/product/admin/add",
                        success: function(layero, index) {
                            window.sessionStorage.setItem('categoryId', '');
                            view(this.id).render('checkinrecord/table').done(function() {
                                form.render(null, 'layuiadmin-form-admin');
                                var aa = layedit.build('demo');
                                //监听提交
                                form.on('submit(LAY-user-front-submit)', function(data) {
                                    var field = data.field; //获取提交的字段
                                    var aahtml = layedit.getContent(aa);
                                    field.introduct = aahtml
                                    field.isHot = data.field.isHot == 'on' ? '1' : 0
                                        //提交 Ajax 成功后，关闭当前弹层并重载表格
                                    adminAdd(field);
                                    layui.table.reload("table-instance");
                                    layer.close(index); //执行关闭 
                                });
                            });
                        }
                    });
                    break;
                case 'getCheckLength':
                    var data = checkStatus.data,
                        arr = [],
                        productId = "";
                    if (data.length == 0) {
                        layer.msg('未选中数据');
                        return;
                    }
                    for (let i = 0; i < data.length; i++) {
                        arr.push(data[i].productId)
                    }
                    var params = {
                        ids: arr
                    }
                    admin.req({
                        url: setter.request.baseUrl + "/product/admin/deleteall",
                        data: JSON.stringify(params),
                        type: "post",
                        headers: {
                            'Content-Type': 'application/json'
                        },
                        done: function(res) {
                            layer.msg("删除成功"); //删除成功之后弹窗
                            table.reload('table-instance', {});
                        },
                    });
                    break;
                case 'sonBtn':
                    admin.popup({
                        title: '添加子目录',
                        area: ['400px', '400px'],
                        offset: 'auto',
                        id: 'LAY-popup-useradmin-add',
                        url: setter.request.baseUrl + "/product/admin/addmenu",
                        success: function(layero, index) {
                            view(this.id).render('checkinrecord/sontable').done(function() {
                                form.render(null, 'layuiadmin-form-admin');
                                var aa = layedit.build('demo');
                                //监听提交
                                form.on('submit(LAY-user-front-submit1)', function(data) {
                                    var field = data.field;
                                    admin.req({
                                        url: setter.request.baseUrl + "/product/admin/addmenu",
                                        data: JSON.stringify(field),
                                        type: "post",
                                        dataType: "json",
                                        contentType: "application/json",
                                        done: function(res) {
                                            if (res.code == 0) {
                                                layer.msg("添加成功"); //删除成功之后弹窗
                                                getmenu()
                                                layer.close(index); //执行关闭 
                                            }
                                        },
                                    });
                                });
                            });
                        }
                    });
            };
        });


        table.render({
            elem: "#table-instance",
            url: setter.request.baseUrl + "/product/admin/search",
            toolbar: '#toolbarDemo', //开启头部工具栏，并为其绑定左侧模板
            method: "get",
            headers: {
                token: layui.data("layuiAdmin").token
            },
            response: {
                statusName: "code",
                statusCode: 0,
                msgName: "msg",
                countName: "count",
                dataName: "list",
            },
            where: {
                typeId: 1
            },
            cols: [
                [{
                    type: 'checkbox',
                    fixed: 'left'
                }, {
                    align: 'center',
                    field: "indexNo",
                    width: "6%",
                    title: "目录号",
                    sort: true
                }, {
                    align: 'center',
                    field: "productName",
                    width: "6.5%",
                    title: "产品名称"
                }, {
                    align: 'center',
                    field: "standards",
                    width: "7%",
                    title: "规格"
                }, {
                    align: 'center',
                    field: "price",
                    width: "5%",
                    title: "价格"
                }, {
                    align: 'center',
                    field: "useBook",
                    width: "7%",
                    title: "使用说明url"
                }, {
                    align: 'center',
                    field: "productImg",
                    width: "6%",
                    templet: "#productImg",
                    title: "产品图片"
                }, {
                    align: 'center',
                    field: "name",
                    width: "12%",
                    title: "名称"
                }, {
                    align: 'center',
                    field: "introduct",
                    width: "6%",
                    title: "简介"
                }, {
                    align: 'center',
                    field: "licence",
                    title: "许可证",
                    templet: "#licence",
                    width: "6%"
                }, {
                    align: 'center',
                    field: "registeNo",
                    templet: "#registeNo",
                    width: "6%",
                    title: "注册证"
                }, {
                    align: 'center',
                    field: "categoryId",
                    width: "5%",
                    title: "类别ID"
                }, {
                    align: 'center',
                    field: "viewCount",
                    width: "6%",
                    title: "浏览数量"
                }, {
                    align: 'center',
                    field: "isHot",
                    width: "6%",
                    title: "是否热门",
                    templet: '#switchTpl',
                    unresize: true,
                    templet: function(res) {
                        var a = '';
                        if (res.isHot == 0) {
                            a = '否'
                        } else if (res.isHot == 1) {
                            a = '是'
                        }
                        return a
                    }
                }, {
                    align: 'center',
                    width: "13%",
                    title: "手动操作",
                    toolbar: "#table-useradmin-webuser" //删除
                }]
            ],
            page: true, //分页
        });
        $('.layui-btn.layuiadmin-btn-useradmin').on('click', function() {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        $(document).on("click", ".table-check-img", function(e) {
            var json = {
                title: "审核图片",
                id: 0,
                start: 0,
                data: [{
                    alt: "",
                    pid: 1,
                    src: $(e.target).data("img"),
                      //原图地址
                    thumb: "",
                }, ],
            };
            console.log(json)
            if (json.data[0].src == null) {
                layer.msg("暂无图片", {
                    offset: "15px",
                    icon: "2",
                    time: 1000
                })
            } else {
                var result = json.data[0].src.split(",");
                var j = new Array;
                for (var i = 0; i < result.length; i++) {
                    let json = {
                        "src": result[i]
                    }
                    j[i] = json;
                }
                layer.photos({
                    photos: {
                        "data": j
                    },
                    anim: 1,
                });
            }
        });

        //监听右侧的删除编辑查看模块
        table.on("tool(table-instance)", function(obj) {
            var data = obj.data;
            if (obj.event === "del") {
                layer.confirm("真的删除么", function(index) {
                    delAdminHandle({
                        productId: data.productId, //接口参数
                    });
                    obj.del();
                    layer.close(index);
                });
            } else if (obj.event === "edit") {
                admin.popup({
                    title: "编辑用户",
                    area: ["900px", "800px"],
                    success: function(layero, index) {
                        //存值
                        window.sessionStorage.setItem('categoryId', data.categoryId);
                        view(this.id)
                            .render("checkinrecord/table", data)
                            .done(function() {
                                let _data = data;
                                console.log(data)
                                form.render(null, "layuiadmin-form-admin");
                                //富文本
                                var aa = layedit.build('demo');
                                layedit.setContent(aa, data.introduct, false)
                                    //监听提交
                                form.on('submit(LAY-user-front-submit)', function(data) {
                                    var field = data.field; //获取提交的字段
                                    var aahtml = layedit.getContent(aa);
                                    field.introduct = aahtml
                                    field.isHot = data.field.isHot == 'on' ? '1' : 0
                                        //提交 Ajax 成功后，关闭当前弹层并重载表格
                                    adminAdd(field);
                                    layui.table.reload("table-instance");
                                    layer.close(index); //执行关闭 
                                });
                            });
                    },
                });
            } else if (obj.event === 'detail') {
                admin.popup({
                    title: '查看',
                    area: ['900px', '900px'],
                    id: 'LAY-popup-useradmin-add',
                    closeBtn: false,
                    // layer.msg('热门：' + data.isHot + ' 的查看操作'); 
                    success: function(layero, index) {
                        // var body = layer.getChildFrame('body',index);//建立父子联系【核心语句】
                        // body.find("input[name='price']").val(data.price)
                        view(this.id)
                            .render("checkinrecord/orderform", data)
                            .done(function() {
                                form.render(null, "layuiadmin-form-admin");
                                //富文本赋值
                                var aa = layedit.build('demo1', '');
                                layedit.setContent(aa, data.introduct, false)
                            });
                    }
                })

            }

        })


        //获取子菜单
        getmenu()

        function getmenu() {
            admin.req({
                url: setter.request.baseUrl + "/product/admin/menu",
                data: {
                    parentId: 3
                },
                type: "get",
                done: function(res) {
                    if (res.code == 0) {
                        var html = "",
                            html1 = "";
                        html1 = "<option value=''>请选择</option>"
                        for (let i = 0; i < res.list.length; i++) {
                            html += "<option value=" + res.list[i].menuId + ">" + res.list[i].menuName + "</option>"
                        }
                        $("#categoryId").empty().append(html1 + html)
                        layui.form.render('select')
                    }
                },
            });
        }
        //删除
        function delAdminHandle(params) {
            admin.req({
                url: setter.request.baseUrl + "/product/admin/delete",
                data: {
                    ...params,
                },
                type: "get",
                done: function(res) {
                    layer.msg("删除成功"); //删除成功之后弹窗
                    table.reload('table-instance', {});
                },
            });
        }
        //修改编辑
        function editUserHandle(params) {
            admin.req({
                url: setter.request.baseUrl + "/recruit/admin/update",
                data: JSON.stringify(params),
                type: "post",
                dataType: "json",
                contentType: "application/json",
                done: function(res) {
                    layui.table.reload("table-instance");
                },
            });
        }

        //添加
        function adminAdd(params) {
            admin.req({
                url: setter.request.baseUrl + "/product/admin/add",
                data: JSON.stringify(params),
                type: "post",
                dataType: "json",
                contentType: "application/json",
                done: function(res) {
                    layui.table.reload("table-instance");
                },
            });
        }
    });
</script>
<script type="text/html" id="productImg">
    <button type="button" class="layui-btn layui-btn-xs table-check-img" data-img="{{d.productImg || d.licence }}">查看图片</button>
</script>
<script type="text/html" id="licence">
    <button type="button" class="layui-btn layui-btn-xs table-check-img" data-img="{{ d.licence }}">查看图片</button>
</script>
<script type="text/html" id="registeNo">
    <button type="button" class="layui-btn layui-btn-xs table-check-img" data-img="{{ d.registeNo }}">查看图片</button>
</script>